package win.smartown.scrollconflict;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import win.smartown.scrollconflict.netease.NeteaseMusicActivity;

public class MainActivity extends AppCompatActivity {

    private CustomViewPager mViewPager;
    private List<Pager> mPagers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(this, NeteaseMusicActivity.class));
        setContentView(R.layout.activity_main);
        mViewPager = findViewById(R.id.view_pager);
        mPagers = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            mPagers.add(new Pager(this, i));
        }
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mPagers.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mPagers.get(position).getViewPager();
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mPagers.get(position).getViewPager());
            }
        });
    }

    public static class Pager {

        private List<View> mViews;
        private InnerViewPager mViewPager;

        public Pager(Context context, int position) {
            mViews = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                TextView textView = new TextView(context);
                textView.setText(position + " - " + i);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                mViews.add(textView);
            }
            mViewPager = new InnerViewPager(context);
            mViewPager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return mViews.size();
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == object;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    View view = mViews.get(position);
                    container.addView(view);
                    return view;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView(mViews.get(position));
                }
            });
        }

        public ViewPager getViewPager() {
            return mViewPager;
        }
    }

}
